package com.pda.xinma.ui.inventory;

import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.pda.xinma.R;
import com.pda.xinma.adapter.InventoryAdapter;
import com.pda.xinma.adapter.InventoryOneAdapter;
import com.pda.xinma.bean.ManifestListBean;
import com.pda.xinma.bean.TabEvent;
import com.pda.xinma.contract.InventoryContract;
import com.pda.xinma.dialog.AffirmDialog;
import com.pda.xinma.dialog.CallUpDialog;
import com.pda.xinma.presenter.InventoryPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.ui.mvp.base.ui.BaseMvpFragment;
import com.wangshen.base.util.SharedPreferencesUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class InventoryFragment extends BaseMvpFragment<InventoryPresenter> implements InventoryContract.View, OnRefreshListener, OnLoadMoreListener {
    InventoryOneAdapter adapter;
    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.srl_layout)
    SmartRefreshLayout srlLayout;
    @BindView(R.id.sb_scan)
    TextView sbScan;
    private String taskNo;
    private int pager = 1;

    public InventoryFragment(String taskNo) {
        this.taskNo = taskNo;
    }

    public InventoryFragment() {
    }

    @Override
    public InventoryPresenter getPresenter() {
        return new InventoryPresenter();
    }

    @Override
    public int getViewId() {
        return R.layout.fragment_list_inventory;
    }

    @Override
    public void initView() {
        srlLayout.setOnRefreshListener(this);
        srlLayout.setOnLoadMoreListener(this);
        srlLayout.autoRefresh();
        adapter = new InventoryOneAdapter(R.layout.item_inverntory_one, new ArrayList<>());
        rvContent.setAdapter(adapter);
        srlLayout.autoRefresh();

    }

    @Override
    public void initLinsenterner() {

        adapter.setOnClickItem(new InventoryOneAdapter.OnClickItem() {
            @Override
            public void onItemClick(ManifestListBean.DetailsBean detailsBean,String id) {
                if (detailsBean.getIsCounted().equals("10") || detailsBean.getIsCounted().equals("20")) {
                    AffirmDialog dialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
                        @Override
                        public void onConfrim() {
                            presenter.callTakeDown(taskNo, id);
                        }
                    });
                    dialog.show("确认要下架吗？");
                }
                if (detailsBean.getIsCounted().equals("40") || detailsBean.getIsCounted().equals("30") || detailsBean.getIsCounted().equals("50")) {
                    EventBus.getDefault().post(new TabEvent(1,id));
                }

            }
        });
    }

    @Override
    public void getData() {

    }

    @Override
    public void showMessage(String msg) {
        super.showMessage(msg);
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public void setDataList(List<ManifestListBean> list) {
        if (pager == 1) {
            adapter.setNewInstance(list);
        } else {
            adapter.addData(list);
        }
        srlLayout.finishRefresh();
        srlLayout.finishLoadMore();
    }

    @Override
    public void onRefresh(RefreshLayout refreshLayout) {
        pager = 1;
        presenter.manifestList(taskNo, pager);
    }

    @Override
    public void onLoadMore(RefreshLayout refreshLayout) {
        pager++;
        presenter.manifestList(taskNo, pager);
    }

    @OnClick(R.id.sb_scan)
    public void onViewClicked() {
        AffirmDialog affirmDialog=new AffirmDialog(mContext, new AffirmDialog.OnConfrim() {
            @Override
            public void onConfrim() {
                presenter.getAllCallTakeDown(taskNo, "");
            }
        });
        affirmDialog.show("确定要下架全部？");
    }
}
